package com.restaurant.ressys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.restaurant.ressys.entity.*;
import com.restaurant.ressys.service.OrderItemService;
import com.restaurant.ressys.service.OrderService;
import com.restaurant.ressys.service.UserService;
import com.restaurant.ressys.util.AlipayTemplate;
import com.restaurant.ressys.util.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yx
 * @since 2022-02-07
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService os;
    @Autowired
    private OrderItemService ois;
    @Autowired
    private UserService us;
    @GetMapping("/selectAll")
    public List<Order> selectAll(){
        List<Order> list = os.list();
        List<User> userList = us.list();
        list=list.stream().peek(order->{
            User member1 = userList.stream().filter((member) -> member.getId() == order.getUid()).collect(Collectors.toList()).get(0);
            order.setUser(member1);
        }).collect(Collectors.toList());
        return list;
    }
    @GetMapping("/selectMyOrder")
    public List<Order> selectMyOrder(HttpSession session){
        User member = (User)session.getAttribute("user");
        QueryWrapper qw=new QueryWrapper();
        qw.eq("uid",member.getId());

        List<Order> list = os.list(qw);
        List<User> memberList = us.list();
        list=list.stream().peek(order->{
            User member1 = memberList.stream().filter((member2) -> member2.getId() == order.getUid()).collect(Collectors.toList()).get(0);
            order.setUser(member1);
        }).collect(Collectors.toList());
        return list;
    }
    @PostMapping("/add")
    public void add(@RequestBody Order order){
        os.save(order);
    }
    @PostMapping("/genateOrder")
    public void genateOrder(HttpSession session){
        User user = (User) session.getAttribute("user");
        Cart cart=(Cart)session.getAttribute("cart");
        Order order = new Order();
        order.setUid(user.getId());
        order.setOrdertime(LocalDateTime.now());
        order.setGoodsprice(cart.getTotalMoney());
        order.setOrderstate(0);
        os.save(order);
        Map<Integer, CartItem> buyMap = cart.getBuyMap();
        for(Map.Entry<Integer, CartItem>  entry:buyMap.entrySet()){
            CartItem ci = entry.getValue();
            OrderItem oi=new OrderItem();
            oi.setCount(ci.getCount());
            oi.setOid(order.getId());
            oi.setFid(entry.getKey());
            ois.save(oi);
        }
        session.removeAttribute("cart");
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") int id){
        os.removeById(id);
    }
    @Autowired
    private AlipayTemplate at;
    @RequestMapping("/pay")
    public void payMoney(PayVo pv, HttpServletResponse response) throws Exception {
        //告知浏览器响应的是一个html字符串
        response.setContentType("text/html");
        String pay = at.pay(pv);//支付的html源码
        response.getWriter().print(pay);
    }
    @RequestMapping("/changeOrderState")
    public String changeOrderState(int out_trade_no, HttpServletRequest request){
        boolean result = at.rsaCheckV1(request);//是否验签通过
        if(result){
            System.out.println("修改订单编号："+out_trade_no+"的订单状态");
            Order order = new Order();
            order.setId(out_trade_no);
            order.setOrderstate(1);
            os.updateById(order);
        }
        return "订单支付成功！";
    }
}

